package gradle.rabbit.common.config;

import gradle.rabbit.common.constants.RabbitContainerConstant;
import gradle.rabbit.common.constants.RabbitTemplateConstant;
import gradle.rabbit.common.properties.PrimaryRabbitProperty;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


/**
 * 首选MQ配置
 *
 * @author 1141193930@qq.com
 */
@Slf4j
@Configuration
@ConditionalOnClass(value = ConnectionFactory.class)
public class PrimaryConnectionConfig {

    /**
     * Rabbit属性配置
     */
    @Resource
    private PrimaryRabbitProperty primaryRabbitProperty;


    /**
     * 创建RabbitMQ连接工厂
     *
     * @return {@link ConnectionFactory}
     */
    @Bean
    @Primary
    public ConnectionFactory primaryConnectionFactory() {
        boolean sslEnabled = primaryRabbitProperty.isSslEnabled();
        boolean sslVerifyHostname = primaryRabbitProperty.isSslVerifyHostname();
        boolean sslValidateServerCertificate = primaryRabbitProperty.isSslValidateServerCertificate();
        // 获取配置Bean
        RabbitConnectionFactoryBean factoryBean = BaseRabbitConnectionFactory.getConnectionFactoryBean(sslEnabled, sslVerifyHostname, sslValidateServerCertificate);
        // 获取配置工厂
        CachingConnectionFactory connectionFactory = BaseRabbitConnectionFactory.getCacheConnectionFactory(factoryBean);
        connectionFactory.setAddresses(primaryRabbitProperty.getAddresses());
        connectionFactory.setUsername(primaryRabbitProperty.getUsername());
        connectionFactory.setPassword(primaryRabbitProperty.getPassword());
        connectionFactory.setVirtualHost(primaryRabbitProperty.getVirtualHost());
        connectionFactory.setConnectionTimeout(primaryRabbitProperty.getConnectionTimeout());
         return connectionFactory;
    }


    /**
     * 注册RabbitMQ消息发送模版
     *
     * @return {@link RabbitTemplate}
     */
    @Primary
    @Bean(name = RabbitTemplateConstant.PRIMARY_RABBIT_TEMPLATE)
    public RabbitTemplate primaryRabbitTemplate() {
        boolean retryEnabled = primaryRabbitProperty.isTemplateRetryEnabled();
        return BaseRabbitConnectionFactory.getRabbitTemplate(retryEnabled, primaryConnectionFactory());
    }


    /**
     * 注册RabbitMQ消费者容器工厂
     *
     * @return {@link SimpleRabbitListenerContainerFactory}
     */
    @Primary
    @Bean(name = RabbitContainerConstant.PRIMARY_CONTAINER)
    public SimpleRabbitListenerContainerFactory primaryListenerContainerFactory() {
        return BaseRabbitConnectionFactory.getRabbitListenerContainer(primaryConnectionFactory());
    }

    /**
     * 注册RabbitMQ批量-消费者容器工厂
     *
     * @return {@link SimpleRabbitListenerContainerFactory}
     */
    @Primary
    @Bean(name = RabbitContainerConstant.PRIMARY_BATCH_CONTAINER)
    public SimpleRabbitListenerContainerFactory primaryBatchListenerContainerFactory() {
        return BaseRabbitConnectionFactory.getBatchRabbitListenerContainer(primaryConnectionFactory());
    }

    /**
     * 注册RabbitAdmin
     *
     * @return {@link RabbitAdmin}
     */
    @Bean
    @Primary
    public RabbitAdmin primaryRabbitAdmin() {
        return BaseRabbitConnectionFactory.getRabbitAdmin(primaryConnectionFactory());
    }

}
